import request from '@renderer/utils/request';
import { useConfigStore } from '@renderer/stores/configStore'

// 辅助函数：延迟获取基础 URL（确保在应用挂载后执行）
const getBaseUrl = () => {
  const configStore = useConfigStore();
  return configStore.baseUrl;
};

export function characterApi() {
    return {
      getCharacterTags: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/get_character_tags', // 调用辅助函数
          method: 'get',
          params: data,
        })
      },
      getCharacterList: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/get_character_paged',
          method: 'get',
          params: data,
        })
      },
      deleteCharacter: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/delete_character',
          method: 'post',
          data: data,
        })
      },
      deleteCharactersByFilter: (params: object) => {
        return request({
          url: getBaseUrl() + 'sql/delete_characters_by_filter',
          method: 'delete',
          params: params,
        })
      },
      deleteDislikedCharacter: () => {
        return request({
          url: getBaseUrl() + 'sql/delete_disliked_character',
          method: 'post',
        })
      },
      updateCharacter: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/update_character',
          method: 'put',
          data: data,
        })
      },
      updateCharacterImage: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/update_character_image',
          method: 'put',
          data: data,
        })
      },
      addCharacter: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/add_character',
          method: 'post',
          data: data,
        })
      },
      exportCharacterStream: (params: any) => {
        return request({
          url: getBaseUrl() + 'sql/export_character_stream',
          method: 'get',
          params: params,
          responseType: 'blob',
        });
      },
      importCharacterCsv: (data: any) => {
        return request({
          url: getBaseUrl() + 'import/character-csv',
          method: 'post',
          data: data,
          headers: { 'Content-Type': undefined }
        });
      },
      importCharacterImages: (data: any) =>{
        return request({
          url: getBaseUrl() + 'sql/batch_add_character',
          method: 'post',
          data: data,
          headers: { 'Content-Type': undefined }
        });
      }
    }
}
    